package com.yzt.dynamicmodule.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.arouter.RouterDynamic;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.event.user.RefreshDynamicEvent;
import com.yzt.zhuju.mvp.MVPFragment;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.dynamic.DynamicBaseBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicListBean;
import com.yzt.zhuju.photoview.PhotoViewManager;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.adapter.DynamicListAdapter;
import com.yzt.dynamicmodule.contacts.DynamicListContacts;
import com.yzt.dynamicmodule.presenter.DynamicListPresenter;
import com.yzt.p_base.AppUtils;
import com.yzt.p_base.utils.ToastUtil;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import rx.functions.Action1;

/**
 * author : bob
 * date   : 2023/2/27
 * desc   : 动态列表
 */
@SuppressLint("SetTextI18n")
public class DynamicListFragment extends MVPFragment<DynamicListPresenter> implements DynamicListContacts.View, OnRefreshListener {

    private DynamicListAdapter adapter;

    private RefreshLayout mRefreshLayout;

    public static DynamicListFragment getInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        DynamicListFragment liveRoomListFragment = new DynamicListFragment();
        liveRoomListFragment.setArguments(args);
        return liveRoomListFragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_dynamic_list;
    }

    @Override
    public void installViews() {

        // 刷新列表
        observerRxBus(RefreshDynamicEvent.class, new Action1<RefreshDynamicEvent>() {
            @Override
            public void call(RefreshDynamicEvent walletEvent) {
                mRefreshLayout.autoRefresh();
            }
        });

        findViewById(R.id.publish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  startActivity(new Intent(DynamicListFragment.this, AddDynamicActivity.class));
            }
        });

        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        mRefreshLayout.setOnRefreshListener(this);

        RecyclerView recycle = findViewById(R.id.blacklist_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new DynamicListAdapter();
        recycle.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter, view, position) -> {
            DynamicListBean entry = (DynamicListBean) adapter.getItem(position);
            RouterDynamic.DynamicDetailActivity(entry);
        });

        adapter.addChildClickViewIds(R.id.img_header);
        adapter.addChildClickViewIds(R.id.tv_follow);
        adapter.addChildClickViewIds(R.id.tv_like);
        adapter.addChildClickViewIds(R.id.dynamic_img);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter1, @NonNull View view, int position) {

                String userId = RXingUtils.getPersonDetail().getBasic().getUserId();

                DynamicListBean entry = adapter.getItem(position);

                // 删除 举报
//                if (view.getId() == R.id.img_more) {
//
//                    MenuSelectDialog menuSelectDialog = new MenuSelectDialog(getActivity(), R.style.ActionSheetDialogStyle);
//
//                    String btn;
//
//                    if (userId.equals(entry.getUserId())) {
//                        btn = getString(R.string.delete);
//                    } else {
//                        btn = getString(R.string.report);
//                    }
//
//                    menuSelectDialog.setLogoutText(btn);
//
//                    menuSelectDialog.setOnItemClickListener(i1 -> {
//                        if (i1 == 1) {
//                            if (!userId.equals(entry.getUserId())) {
//                                // 举报
//                                RouterAccount.ReportActivity(entry.getUserId()+"", entry.getMomentId()+"");
//                                return;
//                            }
//
//                            // 删除
//                            AppDialogUtil.showTitleDialog(mActivity, "", getString(R.string.del_dynamic), mActivity.getString(R.string.cancel), mActivity.getString(R.string.confirm), new AppDialogUtil.DialogClickInterface() {
//                                @Override
//                                public void onCancelClick() {
//
//                                }
//
//                                @Override
//                                public void onSureClick() {
//                                    mPresenter.delDynamic(entry.getMomentId()+"");
//                                }
//                            });
//                        }
//                    });
//                }

                // 关注
                if (view.getId() == R.id.tv_follow) {
                    mPresenter.followUser(entry.getUserId() + "", !entry.isFollowed(), position);
                }

                // 用户详情
                if (view.getId() == R.id.img_header) {
                    RouterUser.PersonalDetailActivity(entry.getUserId() + "", "动态");
                }

                // 点赞
                if (view.getId() == R.id.tv_like) {
                    mPresenter.dynamicLike(entry.getMomentId() + "", !entry.isLiked(), position);
                }

                // 图片
                if (view.getId() == R.id.dynamic_img) {
                    if (entry.getPicList() == null || entry.getPicList().size() == 0) {
                        return;
                    }
                    PhotoViewManager.getInstance(AppUtils.getApp()).showPhotoView(view, entry.getPicList(), 0);
                }
            }
        });

        adapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            getHttpData();
        });

        mRefreshLayout.autoRefresh();
    }

    @Override
    public void netWorkRequest() {

    }

    /**
     * 获取动态列表
     */
    public void getHttpData() {

        BizLayer.getInstance().getDynamicModule().dynamicList(mCurrentPage, PAGE_SIZE, new AresSubscriber<DynamicBaseBean>() {
            @Override
            protected void onSuccess(DynamicBaseBean bean) {
                List<DynamicListBean> list = bean.getMomentList();
                if (mCurrentPage == 1) {
                    if (list.size() == 0) {
                        adapter.setEmptyView(getEmptyView());
                    } else {
                        adapter.removeEmptyView();
                    }
                    adapter.setList(list);
                } else {
                    adapter.addData(list);
                }
                adapter.getLoadMoreModule().loadMoreComplete();
                if (list.size() < PAGE_SIZE) {
                    adapter.getLoadMoreModule().loadMoreEnd();
                }
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (mCurrentPage == 1) {
                    adapter.setEmptyView(getErrorView(info));
                }
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                mRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        getHttpData();
    }

    @Override
    public void delDynamic() {
        ToastUtil.showMessage(getString(R.string.dynamic_del_success));
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void dynamicLike(boolean like, int position) {
        DynamicListBean entry = adapter.getItem(position);
        entry.setLiked(like);
        if (entry.isLiked()) {
            entry.setLikeCount(entry.getLikeCount() + 1);
        } else {
            entry.setLikeCount(entry.getLikeCount() - 1);
        }
        adapter.notifyItemChanged(position);
    }

    @Override
    public void followUser(int position) {
        DynamicListBean entry = adapter.getItem(position);
        entry.setFollowed(true);
        adapter.notifyItemChanged(position);
        ToastUtil.showMessage(getString(R.string.user_follow_success));
    }
}